package com.cn.mmd.controller;

import com.cn.mmd.model.*;
import com.cn.mmd.realm.SecurityRealm;
import com.cn.mmd.service.*;
import com.cn.mmd.tools.GetLimitNum;
import net.sf.json.JSONArray;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 对教师评价的控制类
 * Created by Administrator on 2016/7/8.
 */

@Controller
@RequestMapping(value = "EvaluateTeacher")
public class EvaluateTeacherController {

    @Autowired
    EvaluateTeacherService evaluateTeacherService;
    @Autowired
    TeacherService teacherService;
    @Autowired
    CourseService courseService;
    @Autowired
    LabelService labelService;
    @Autowired
    TokenController tokenController;
    @Autowired
    StudentCustomEvaluationService studentCustomEvaluationService;
    @Autowired
    EvaluateStaService evaluateStaService;


//    /**
//     * 总平均分与我的平均分概况
//     *
//     * @param request
//     * @return
//     */
//    @RequestMapping(value = "forMyEvaluation")//这里需要修改！！！！！！！！！！！！！！！！！！！！！！！！！！11
//    public synchronized @ResponseBody Map<String, Object> forMyEvaluation(HttpServletRequest request) {
//        Map<String, Object> map = new HashMap<String, Object>();
//        Map<String, Object> map1 = new HashMap<String, Object>();
//
//
//        String teacherId = request.getParameter("teacherId");//传入“我”的工号
//        String token=request.getParameter("token");
//
//        TeacherData teacherData;
//        teacherData = (TeacherData) tokenController.token(teacherId, token);
//        if (teacherData==null){
//            map.put("code", 205);
//            map.put("message", "您暂无权限做此操作");
//            map.put("data", "");
//            return map;
//        }
//
//       // TeacherRanking teacherRanking = evaluateTeacherService.selecteMyAvg(teacherId);
//        List<TeacherRanking> list=evaluateTeacherService.selectAllAvg();
//
//
//        if (list.size()>0) {
//            //排序！！
//            for (int i=0;i<list.size();i++){
//                for (int j=i+1;j<list.size();j++){
//                    if (list.get(i).getTeacheravg()<list.get(j).getTeacheravg()){
//                        TeacherRanking teacherRanking1=list.get(i);
//                        list.set(i, list.get(j));
//                        list.set(j,teacherRanking1);
//                    }
//                }
//            }
//            for (int x=0;x<list.size();x++){
//                if (list.get(x).getTeacherid().equals(teacherId)){
//                    int teaRanking=x+1;
//                    map1.put("teaRanking",teaRanking);//我的排名
//                    map1.put("teacherAvg",list.get(x).getTeacheravg());//我的总平均分
//                }
//            }
//
//            AvgSchool avgSchool=teacherService.selectAvgSchool();
//            if (avgSchool!=null){
//
//                float schoolAvg=(float)(Math.round((avgSchool.getSchoolsum()/avgSchool.getSchoolheadcount())*100))/100;
//                map1.put("schoolAvg",schoolAvg);//学校总平均分
//
//                float studentSchoolAvg=(float)(Math.round((avgSchool.getStudentschoolsum()/avgSchool.getStudentschoolheadcount())*100))/100;
//                map1.put("studentSchoolAvg",studentSchoolAvg);//学生对整个学校老师的平均分
//
//                float colleagueSchoolAvg=(float)(Math.round((avgSchool.getColleagueschoolsum()/avgSchool.getColleagueschoolheadcount())*100))/100;
//                map1.put("colleagueSchoolAvg",colleagueSchoolAvg);//同事对整个学校老师的平均分
//
//                float supervisorSchoolAvg=(float)(Math.round((avgSchool.getSupervisorschoolsum()/avgSchool.getSupervisorschoolheadcount())*100))/100;
//                map1.put("supervisorSchoolAvg",supervisorSchoolAvg);//督导对整个学校老师的平均分
//            }else {
//                map.put("code", 201);
//                map.put("message", "查询学校失败");
//                map.put("data", "");
//                return map;
//            }
//
//            Teacher teacher=teacherService.serlectTeacher(teacherId);//查出所在学院
//            if (teacher!=null){
//
//                AvgCollege avgCollege=teacherService.selectAvgCollege(teacher.getTeachercollege());//查出该学院分数
//                if (avgCollege!=null){
//
//                    float collegeAvg=(float)(Math.round((avgCollege.getCollegesum()/avgCollege.getCollegeheadcount())*100))/100;
//                    map1.put("collegeAvg",collegeAvg);//学院总平均分
//
//                    float studentCollegeAvg=(float)(Math.round((avgCollege.getStudentcollegesum()/avgCollege.getStudentcollegeheadcount())*100))/100;
//                    map1.put("studentCollegeAvg",studentCollegeAvg);//学生对整个学院老师的平均分
//
//                    float colleagueCollegeAvg=(float)(Math.round((avgCollege.getColleaguecollegesum()/avgCollege.getColleaguecollegeheadcount())*100))/100;
//                    map1.put("colleagueCollegeAvg",colleagueCollegeAvg);//同事对整个学院老师的平均分
//
//                    float supervisorCollegeAvg=(float)(Math.round((avgCollege.getSupervisorcollegesum()/avgCollege.getSupervisorcollegeheadcount())*100))/100;
//                    map1.put("supervisorCollegeAvg",supervisorCollegeAvg);//督导对整个学院老师的平均分
//
//                }else{
//                    map.put("code", 202);
//                    map.put("message", "查询学院失败");
//                    map.put("data", "");
//                    return map;
//                }
//
//            }else{
//                map.put("code", 203);
//                map.put("message", "查询工号失败");
//                map.put("data", "");
//                return map;
//            }
//            map.put("code", 200);
//            map.put("message", "查询成功");
//            map.put("data", map1);
//
//
//        } else {//如果查询无记录
//            map.put("code", 204);
//            map.put("message", "教师排名查询失败");
//            map.put("data", "");
//
//        }
//
//        return map;
//    }


    /**
     * 查看对老师的同事评价与督导评价
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "teacherEvaluation")
    public synchronized @ResponseBody Map<String, Object> teacherEvaluation(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();

        List<Map<String, Object>> mapList=new ArrayList<Map<String, Object>>();


        //前端传入被评价人的id
        String teacherId = request.getParameter("teacherId");
        String evaluateType = request.getParameter("evaluateType");//查看的评价类型  同事评价为colleagueEvaluation，督导评价为supervisorEvaluation

        String token=request.getParameter("token");

        TeacherData teacherData;
        teacherData = (TeacherData) tokenController.token(teacherId, token);
        if (teacherData==null){
            map.put("code", 201);
            map.put("message", "您暂无权限做此操作");
           // map.put("data", "");
            return map;
        }
//
        //查出所有评价的一级指标的分数

        if (evaluateType.equals("colleagueEvaluation")){//同事评价

//            System.out.println("进入同事评价");
            List<LabelTwoIndex> labelTwoIndexList=new ArrayList<LabelTwoIndex>();
            TeacherRanking teacherRanking=teacherService.selectTeacherRanking(teacherId);

            if (teacherRanking!=null&&teacherRanking.getColleaguetwoindexscore()!=null){

//                System.out.println("同事评价："+teacherRanking.getColleaguetwoindexscore());//
                String[] colleagueTwoIndexes=teacherRanking.getColleaguetwoindexscore().split("!!");
                LabelTwoIndex labelTwoIndex=null;
                for (String s:colleagueTwoIndexes){

                    labelTwoIndex=new LabelTwoIndex();
                    String[] s1=s.split("_");
                    String twoIndexName=s1[0];//二级指标名
                    float twoIndexScore=Float.parseFloat(s1[1])/Float.parseFloat(s1[2]);//二级指标得分
                    labelTwoIndex.setTwoindexname(twoIndexName);

                    List<LabelTwoIndex> labelTwoIndexList1=labelService.selectBylabelName(labelTwoIndex);

                    labelTwoIndexList1.get(0).setTwoindexscore(twoIndexScore);

                    labelTwoIndexList.add(labelTwoIndexList1.get(0));//把所有二级指标放入对象中再存入list里
                }

                Map<String,Object> map2=null;
                for (int a=0;a<labelTwoIndexList.size();a++){
                    map2=new HashMap<String, Object>();
                    List<Float> scores = new ArrayList<Float>();
                    List<String> twoIndexNames = new ArrayList<String>();
                    for (int b=labelTwoIndexList.size()-1;b>a;b--){
                        if (labelTwoIndexList.get(a).getIndexname().equals(labelTwoIndexList.get(b).getIndexname())){

                            float score1=(float)(Math.round(labelTwoIndexList.get(b).getTwoindexscore()*labelTwoIndexList.get(b).getProportion()*100))/100;
                            scores.add(score1);
                            twoIndexNames.add(labelTwoIndexList.get(b).getTwoindexname());
                            labelTwoIndexList.remove(b);
                        }
                    }
                    float score1=(float)(Math.round(labelTwoIndexList.get(a).getTwoindexscore()*labelTwoIndexList.get(a).getProportion()*100))/100;
                    scores.add(score1);
                    twoIndexNames.add(labelTwoIndexList.get(a).getTwoindexname());

                    float sun=0f;
                    for (float f:scores){
                        sun+=f;
                    }
                    System.out.println(labelTwoIndexList.get(a).getIndexname()+"==="+sun);
                    map2.put("indexName",labelTwoIndexList.get(a).getIndexname());//一级指标名
                    map2.put("indexScore",sun);//一级指标得分
                    map2.put("twoIndexList",twoIndexNames);//所属的二级指标list

                    mapList.add(map2);
                }

                map.put("code", 200);
                map.put("message", "查看同事评价成功");
                map.put("data", mapList);

            }else {
                String packageName="教评";
                Map<String,Object> map1=null;
                List<String> stringList=null;
                List<LabelIndex> labelIndexList=labelService.selectByPackageName(packageName);
                for (LabelIndex labelIndex:labelIndexList){
                    if (labelIndex.getIndexid()!=5){//不要对学校评价的那个一级指标
                        LabelTwoIndex labelTwoIndex=new LabelTwoIndex();
                        labelTwoIndex.setIndexname(labelIndex.getIndexname());
                        List<LabelTwoIndex> labelTwoIndexList1=labelService.selectBylabelName(labelTwoIndex);//查到二级指标
                        for (LabelTwoIndex labelTwoIndex1:labelTwoIndexList1){
                            stringList=new ArrayList<String>();

                            stringList.add(labelTwoIndex1.getTwoindexname());
                        }
                        map1=new HashMap<String, Object>();
                        map1.put("indexName",labelIndex.getIndexname());//一级指标名
                        map1.put("indexScore",0);//一级指标得分
                        map1.put("twoIndexList",stringList);//所属的二级指标list

                        mapList.add(map1);
                    }
                }

                map.put("code", 200);
                map.put("message", "暂无同事评价数据");
                map.put("data", mapList);
            }

        }else if (evaluateType.equals("supervisorEvaluation")){//督导评价

            List<LabelTwoIndex> labelTwoIndexList=new ArrayList<LabelTwoIndex>();
            TeacherRanking teacherRanking=teacherService.selectTeacherRanking(teacherId);

            if (teacherRanking!=null&&teacherRanking.getSupervisortwoindexscore()!=null){

//                System.out.println("督导评价："+teacherRanking.getSupervisortwoindexscore());
                String[] supervisorTwoIndexes=teacherRanking.getSupervisortwoindexscore().split("!!");
                LabelTwoIndex labelTwoIndex=null;
                for (String s:supervisorTwoIndexes){

                    labelTwoIndex=new LabelTwoIndex();
                    String[] s1=s.split("_");
                    String twoIndexName=s1[0];//二级指标名
                    float twoIndexScore=Float.parseFloat(s1[1])/Float.parseFloat(s1[2]);//二级指标得分
                    labelTwoIndex.setTwoindexname(twoIndexName);

                    List<LabelTwoIndex> labelTwoIndexList1=labelService.selectBylabelName(labelTwoIndex);

                    labelTwoIndexList1.get(0).setTwoindexscore(twoIndexScore);

                    labelTwoIndexList.add(labelTwoIndexList1.get(0));//把所有二级指标放入对象中再存入list里
                }

                Map<String,Object> map2=null;
                for (int a=0;a<labelTwoIndexList.size();a++){
                    map2=new HashMap<String, Object>();
                    List<Float> scores = new ArrayList<Float>();
                    List<String> twoIndexNames = new ArrayList<String>();
                    for (int b=labelTwoIndexList.size()-1;b>a;b--){
                        if (labelTwoIndexList.get(a).getIndexname().equals(labelTwoIndexList.get(b).getIndexname())){

                            float score1=(float)(Math.round(labelTwoIndexList.get(b).getTwoindexscore()*labelTwoIndexList.get(b).getProportion()*100))/100;
//                            float score1=labelTwoIndexList.get(b).getTwoindexscore()*labelTwoIndexList.get(b).getProportion();
                            scores.add(score1);
                            twoIndexNames.add(labelTwoIndexList.get(b).getTwoindexname());
                            labelTwoIndexList.remove(b);
                        }
                    }
                    float score1=(float)(Math.round(labelTwoIndexList.get(a).getTwoindexscore()*labelTwoIndexList.get(a).getProportion()*100))/100;
                    scores.add(score1);
                    twoIndexNames.add(labelTwoIndexList.get(a).getTwoindexname());

                    float sun=0f;
                    for (float f:scores){
                        sun+=f;
                    }

                    map2.put("indexName",labelTwoIndexList.get(a).getIndexname());//一级指标名
                    map2.put("indexScore",sun);//一级指标得分
                    map2.put("twoIndexList",twoIndexNames);//所属的二级指标list

                    mapList.add(map2);
                }

                map.put("code", 200);
                map.put("message", "查看督导评价成功");
                map.put("data", mapList);

            }else {
                String packageName="教评";
                Map<String,Object> map1=null;
                List<String> stringList=null;
                List<LabelIndex> labelIndexList=labelService.selectByPackageName(packageName);
                for (LabelIndex labelIndex:labelIndexList){
                    if (labelIndex.getIndexid()!=5){//不要对学校评价的那个一级指标
                        LabelTwoIndex labelTwoIndex=new LabelTwoIndex();
                        labelTwoIndex.setIndexname(labelIndex.getIndexname());
                        List<LabelTwoIndex> labelTwoIndexList1=labelService.selectBylabelName(labelTwoIndex);//查到二级指标
                        for (LabelTwoIndex labelTwoIndex1:labelTwoIndexList1){
                            stringList=new ArrayList<String>();

                            stringList.add(labelTwoIndex1.getTwoindexname());
                        }
                        map1=new HashMap<String, Object>();
                        map1.put("indexName",labelIndex.getIndexname());//一级指标名
                        map1.put("indexScore",0);//一级指标得分
                        map1.put("twoIndexList",stringList);//所属的二级指标list

                        mapList.add(map1);
                    }
                }

                map.put("code", 200);
                map.put("message", "暂无督导评价数据");
                map.put("data", mapList);
            }
        }

        return map;
    }


    /**
     * 查看对老师的同事评价与督导评价二级指标
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "teacherEvaluateTwoIndex")
    public synchronized @ResponseBody Map<String, Object> teacherEvaluateTwoIndex(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> map1 = new HashMap<String, Object>();
        List<Map> mapList=new ArrayList<Map>();

        String teacherId = request.getParameter("teacherId");
        String evaluateType = request.getParameter("evaluateType");//查看的评价类型  同事评价为colleagueEvaluation，督导评价为supervisorEvaluation

        String indexName=request.getParameter("indexName");//一级指标名


        String token=request.getParameter("token");

        TeacherData teacherData;
        teacherData = (TeacherData) tokenController.token(teacherId, token);
        if (teacherData==null){
            map.put("code", 201);
            map.put("message", "您暂无权限做此操作");
          //  map.put("data", "");
            return map;
        }
        List<String> stringList=new ArrayList<String>();
        LabelTwoIndex labelTwoIndex=new LabelTwoIndex();
        labelTwoIndex.setIndexname(indexName);
        List<LabelTwoIndex> labelTwoIndexList=labelService.selectBylabelName(labelTwoIndex);
        if (labelTwoIndexList.size()>0){
            for (LabelTwoIndex labelTwoIndex1:labelTwoIndexList){
                stringList.add(labelTwoIndex1.getTwoindexname());
            }
        }else{
            map.put("code", 203);
            map.put("message", "一级指标名传入错误");
            return map;
        }

        TeacherRanking teacherRanking=teacherService.selectTeacherRanking(teacherId);

        if (evaluateType.equals("colleagueEvaluation")){//同事评价

            if (teacherRanking!=null&&teacherRanking.getColleaguetwoindexscore()!=null){

                String[] colleagueTwoIndexes=teacherRanking.getColleaguetwoindexscore().split("!!");

                for (String s:colleagueTwoIndexes){
                    Map<String,Object> map2=new HashMap<String, Object>();
                    String[] s1=s.split("_");
                    String twoIndexName=s1[0];
                    float twoIndexScore=(float)(Math.round(Float.parseFloat(s1[1])/Float.parseFloat(s1[2])*100))/100;

                    if (stringList.contains(twoIndexName)){
                        map2.put("twoIndexName",twoIndexName);
                        map2.put("twoIndexScore",twoIndexScore);
                        mapList.add(map2);
                    }
                }
            }else {
                map.put("code", 202);
                map.put("message", "查询无数据");
//                map.put("data", mapList);
            }
        }else if (evaluateType.equals("supervisorEvaluation")){

            if (teacherRanking!=null&&teacherRanking.getSupervisortwoindexscore()!=null){

                String[] supervisorTwoIndexes=teacherRanking.getSupervisortwoindexscore().split("!!");

                for (String s:supervisorTwoIndexes){
                    Map<String,Object> map2=new HashMap<String, Object>();
                    String[] s1=s.split("_");
                    String twoIndexName=s1[0];
                    float twoIndexScore=(float)(Math.round(Float.parseFloat(s1[1])/Float.parseFloat(s1[2])*100))/100;
//                    float twoIndexScore=Float.parseFloat(s1[1])/Float.parseFloat(s1[2]);

                    if (stringList.contains(twoIndexName)){
                        map2.put("twoIndexName",twoIndexName);
                        map2.put("twoIndexScore",twoIndexScore);
                        mapList.add(map2);
                    }
                }
            }else {
                map.put("code", 202);
                map.put("message", "查询无数据");
//                map.put("data", mapList);
            }
        }
        map.put("code", 200);
        map.put("message", "查询成功");
        map.put("data", mapList);

        return map;
    }

    /**
     * 学生端查看对老师的所有自定义评价详情(最新)
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "CustomEvaluation")
    public synchronized @ResponseBody Map<String, Object> CustomEvaluation(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> map1 = new HashMap<String, Object>();

        String teacherId=request.getParameter("teacherId");//教师工号
        int startrow=Integer.parseInt(request.getParameter("startrow"));//查看第几页
        int pageSize=Integer.parseInt(request.getParameter("pageSize"));//一页查看几条
        String operatorId=request.getParameter("operatorId");//操作人id
        String operatorType=request.getParameter("operatorType");//操作人类型
        String token=request.getParameter("token");

        if (operatorType.equals("教师")){

            TeacherData teacherData;
            teacherData = (TeacherData) tokenController.token(operatorId, token);
            if (teacherData==null){
                map.put("code", 203);
                map.put("message", "您暂无权限做此操作");
//                map.put("data", "");
                return map;
            }

        }else if (operatorType.equals("学生")){
            StudentApp studentApp;
            studentApp = (StudentApp) tokenController.token1(operatorId, token);
            if (studentApp==null){
                map.put("code", 203);
                map.put("message", "您暂无权限做此操作");
//                map.put("data", "");
                return map;
            }
        }

        //从第几条开始看  startrow * pageSize

        int num=evaluateTeacherService.countCustomEvaluation(teacherId);
        // System.out.println("num的数量："+num);

        GetLimitNum G=new GetLimitNum();
        int yeshu=G.getLimitNum(num, pageSize);

        // System.out.println("num的数量+："+num);
        int page=(startrow-1)*pageSize;//从哪一条开始
        // System.out.println("page的值："+page);
        if(startrow>yeshu){
            map.put("code",204);
            map.put("message","已没有更多评价");
//            map.put("data","");
            return map;
        }

        CustomEvaluation customEvaluation=new CustomEvaluation(teacherId,page,pageSize);
        List<CustomEvaluation> list=evaluateTeacherService.selectAllCustomEvaluation(customEvaluation);
        if (list.size()>0){
            // List<Map<String,Object>> mapList=new ArrayList<Map<String, Object>>();
            List<Map> mapList = new ArrayList<Map>();
            Map<String , Object> map2 ;
            for (int i = 0 ; i < list.size(); i++){
                customEvaluation = list.get(i);
                map2 = new HashMap<String, Object>();
                map2.put("appraiserid",customEvaluation.getAppraiserid());
                map2.put("byevaluatepersonid",customEvaluation.getByevaluatepersonid());
                map2.put("mynickname",customEvaluation.getMynickname());
                map2.put("appraiserurl",customEvaluation.getAppraiserurl());
                map2.put("evaluationtime",customEvaluation.getEvaluationtime());
                map2.put("evaluationcontent",customEvaluation.getEvaluationcontent());
                map2.put("customevaluationid",customEvaluation.getCustomevaluationid());
                map2.put("likenum",customEvaluation.getLikenum());
                StudentCustomEvaluation studentCustomEvaluation = new StudentCustomEvaluation();
                studentCustomEvaluation.setStudentid(operatorId);
                studentCustomEvaluation.setCustomevaluationid(customEvaluation.getCustomevaluationid());
                List<StudentCustomEvaluation> studentCustomEvaluationList = studentCustomEvaluationService.selectBy(studentCustomEvaluation);
                if (studentCustomEvaluationList!=null&&studentCustomEvaluationList.size()>0){
                    map2.put("likenumtype",1);
                }else {
                    map2.put("likenumtype",0);
                }
                if (customEvaluation.getAppraiserid().equals(operatorId)){
                    map2.put("deltype",1);
                }else {
                    map2.put("deltype",0);
                }
                mapList.add(map2);
            }
            map1.put("list",mapList);
            map1.put("evaluateNum",num);
            map1.put("pages",yeshu);
            // mapList.add(map1);
            map.put("code",200);
            map.put("message","查询成功");
            map.put("data",map1);
        }else {

            TeacherData teacher=teacherService.selectByTeacherId(teacherId);

            if (teacher!=null){
                map.put("code",201);
                map.put("message","该教师未被评价过");
//                map.put("data","");
            }else {
                map.put("code",202);
                map.put("message","未查到该教师");
//                map.put("data","");
            }
        }

        return map;
    }


    /**
     * 对自定义评价点赞
     * @param request
     * @return
     */
    @RequestMapping(value = "likeNum")
    public synchronized @ResponseBody Map<String, Object> likeNum(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        String operatorId=request.getParameter("operatorId");//操作人id
        Integer customEvaluationId = Integer.parseInt(request.getParameter("customEvaluationId"));//自定义的id
        String  byEvaluatePersonId=request.getParameter("byEvaluatePersonId");//被评论人id
        String token=request.getParameter("token");
        StudentApp studentApp;
        studentApp = (StudentApp) tokenController.token1(operatorId, token);
        if (studentApp==null){
            map.put("code", 203);
            map.put("message", "您暂无权限做此操作");
            return map;
        }
        StudentCustomEvaluation studentCustomEvaluation = new StudentCustomEvaluation();
        studentCustomEvaluation.setStudentid(operatorId);
        studentCustomEvaluation.setCustomevaluationid(customEvaluationId);
        List<StudentCustomEvaluation> studentCustomEvaluationList = studentCustomEvaluationService.selectBy(studentCustomEvaluation);
        if (studentCustomEvaluationList!=null&&studentCustomEvaluationList.size()>0){
            map.put("code", 202);
            map.put("message", "请不要重复点赞");
        }else {
            studentCustomEvaluation.setType("点赞");
            Date date = new Date();
            SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String logTime=time.format(date);
            studentCustomEvaluation.setTime(logTime);
            studentCustomEvaluation.setByevaluatepersonid(byEvaluatePersonId);
            CustomEvaluation customEvaluation = studentCustomEvaluationService.selectByPrimaryKeyC(customEvaluationId);
            CustomEvaluation customEvaluation1 = new CustomEvaluation();
            if (customEvaluation!=null) {
                customEvaluation1.setLikenum(customEvaluation.getLikenum() + 1);
                customEvaluation1.setCustomevaluationid(customEvaluationId);
            }
            if (studentCustomEvaluationService.insertSelective(studentCustomEvaluation) > 0&&studentCustomEvaluationService.updateByPrimaryKeySelective(customEvaluation1)>0) {
                map.put("code", 200);
                map.put("message", "成功");
            } else {
                map.put("code", 201);
                map.put("message", "点赞失败");
            }

        }
        return map;
    }

    /**
     * 取消点赞
     * @param request
     * @return
     */
    @RequestMapping(value = "delLikeNum")
    public synchronized @ResponseBody Map<String, Object> delLikeNum(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        String operatorId=request.getParameter("operatorId");//操作人id
        Integer customEvaluationId = Integer.parseInt(request.getParameter("customEvaluationId"));//自定义的id
        String token=request.getParameter("token");
        StudentApp studentApp;
        studentApp = (StudentApp) tokenController.token1(operatorId, token);
        if (studentApp==null){
            map.put("code", 203);
            map.put("message", "您暂无权限做此操作");
            return map;
        }
        StudentCustomEvaluation studentCustomEvaluation = new StudentCustomEvaluation();
        studentCustomEvaluation.setStudentid(operatorId);
        studentCustomEvaluation.setCustomevaluationid(customEvaluationId);
        List<StudentCustomEvaluation> studentCustomEvaluationList = studentCustomEvaluationService.selectBy(studentCustomEvaluation);
        if (studentCustomEvaluationList!=null&&studentCustomEvaluationList.size()>0){
            int num = 0;
            for (int i = 0 ; i < studentCustomEvaluationList.size() ; i++){
                studentCustomEvaluation = studentCustomEvaluationList.get(i);
                if (studentCustomEvaluationService.deleteByPrimaryKey(studentCustomEvaluation.getId())>0){
                    num++;
                }
            }
            CustomEvaluation customEvaluation = studentCustomEvaluationService.selectByPrimaryKeyC(customEvaluationId);
            CustomEvaluation customEvaluation1 = new CustomEvaluation();
            if (customEvaluation!=null&&customEvaluation.getLikenum()>0) {
                customEvaluation1.setLikenum(customEvaluation.getLikenum() - 1);
                customEvaluation1.setCustomevaluationid(customEvaluationId);
                studentCustomEvaluationService.updateByPrimaryKeySelective(customEvaluation1);
            }
            if (num>0){
                map.put("code", 200);
                map.put("message", "成功");
            }else {
                map.put("code", 201);
                map.put("message", "取消失败");
            }
        }else {
            map.put("code", 202);
            map.put("message", "失败没有找到对此评价的点赞");
        }
        return map;
    }

    /**
     * 根据id删除自己的评论
     * @param request
     * @return
     */
    @RequestMapping(value = "delCustomEvaluation")
    public synchronized @ResponseBody Map<String, Object> del(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        String operatorId=request.getParameter("operatorId");//操作人id
        Integer customEvaluationId = Integer.parseInt(request.getParameter("customEvaluationId"));//自定义的id
        String token=request.getParameter("token");
        StudentApp studentApp;
        studentApp = (StudentApp) tokenController.token1(operatorId, token);
        if (studentApp==null){
            map.put("code", 203);
            map.put("message", "您暂无权限做此操作");
            return map;
        }
        if (studentCustomEvaluationService.selectByPrimaryKeyC(customEvaluationId)!=null&&studentCustomEvaluationService.selectByPrimaryKeyC(customEvaluationId).getAppraiserid().equals(operatorId)){
            if (studentCustomEvaluationService.deleteByPrimaryKeyC(customEvaluationId)>0){
                map.put("code", 200);
                map.put("message", "成功");
            }else {
                map.put("code", 201);
                map.put("message", "删除失败");
            }
        }else {
            map.put("code", 202);
            map.put("message", "无权删除此评价");
        }
        return map;
    }


    /**
     * 学生端查看对老师的所有自定义评价详情(最热)
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "CustomEvaluationTop")
    public synchronized @ResponseBody Map<String, Object> CustomEvaluationTop(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> map1 = new HashMap<String, Object>();

        String teacherId=request.getParameter("teacherId");//教师工号
        int startrow=Integer.parseInt(request.getParameter("startrow"));//查看第几页
        int pageSize=Integer.parseInt(request.getParameter("pageSize"));//一页查看几条
        String operatorId=request.getParameter("operatorId");//操作人id
        String operatorType=request.getParameter("operatorType");//操作人类型
        String token=request.getParameter("token");

        if (operatorType.equals("教师")){

            TeacherData teacherData;
            teacherData = (TeacherData) tokenController.token(operatorId, token);
            if (teacherData==null){
                map.put("code", 203);
                map.put("message", "您暂无权限做此操作");
//                map.put("data", "");
                return map;
            }

        }else if (operatorType.equals("学生")){
            StudentApp studentApp;
            studentApp = (StudentApp) tokenController.token1(operatorId, token);
            if (studentApp==null){
                map.put("code", 203);
                map.put("message", "您暂无权限做此操作");
//                map.put("data", "");
                return map;
            }
        }

        //从第几条开始看  startrow * pageSize

        int num=evaluateTeacherService.countCustomEvaluation(teacherId);
        // System.out.println("num的数量："+num);

        GetLimitNum G=new GetLimitNum();
        int yeshu=G.getLimitNum(num, pageSize);

        // System.out.println("num的数量+："+num);
        int page=(startrow-1)*pageSize;//从哪一条开始
        // System.out.println("page的值："+page);
        if(startrow>yeshu){
            map.put("code",204);
            map.put("message","已没有更多评价");
            map.put("data","");
            return map;
        }

        CustomEvaluation customEvaluation=new CustomEvaluation(teacherId,page,pageSize);
        List<CustomEvaluation> list=studentCustomEvaluationService.selectAllByLikeNum(customEvaluation);
        if (list.size()>0){
            // List<Map<String,Object>> mapList=new ArrayList<Map<String, Object>>();
            List<Map> mapList = new ArrayList<Map>();
            Map<String , Object> map2 ;
            for (int i = 0 ; i < list.size(); i++){
                customEvaluation = list.get(i);
                map2 = new HashMap<String, Object>();
                map2.put("appraiserid",customEvaluation.getAppraiserid());
                map2.put("byevaluatepersonid",customEvaluation.getByevaluatepersonid());
                map2.put("mynickname",customEvaluation.getMynickname());
                map2.put("appraiserurl",customEvaluation.getAppraiserurl());
                map2.put("evaluationtime",customEvaluation.getEvaluationtime());
                map2.put("evaluationcontent",customEvaluation.getEvaluationcontent());
                map2.put("customevaluationid",customEvaluation.getCustomevaluationid());
                map2.put("likenum",customEvaluation.getLikenum());
                StudentCustomEvaluation studentCustomEvaluation = new StudentCustomEvaluation();
                studentCustomEvaluation.setStudentid(operatorId);
                studentCustomEvaluation.setCustomevaluationid(customEvaluation.getCustomevaluationid());
                List<StudentCustomEvaluation> studentCustomEvaluationList = studentCustomEvaluationService.selectBy(studentCustomEvaluation);
                if (studentCustomEvaluationList!=null&&studentCustomEvaluationList.size()>0){
                    map2.put("likenumtype",1);
                }else {
                    map2.put("likenumtype",0);
                }
                if (customEvaluation.getAppraiserid().equals(operatorId)){
                    map2.put("deltype",1);
                }else {
                    map2.put("deltype",0);
                }
                mapList.add(map2);
            }
            map1.put("list",mapList);
            map1.put("evaluateNum",num);
            map1.put("pages",yeshu);
            // mapList.add(map1);
            map.put("code",200);
            map.put("message","查询成功");
            map.put("data",map1);
        }else {

            TeacherData teacher=teacherService.selectByTeacherId(teacherId);

            if (teacher!=null){
                map.put("code",201);
                map.put("message","该教师未被评价过");
//                map.put("data","");
            }else {
                map.put("code",202);
                map.put("message","未查到该教师");
//                map.put("data","");
            }
        }

        return map;
    }


    /**
     * 导出学生标签评价数据至Excel表格
     */
    @RequestMapping(value = "ExportLabelEvaluate")
    public synchronized @ResponseBody Map<String,Object>  ExportLabelEvaluate(HttpServletRequest request,HttpServletResponse response) throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> map1 = new HashMap<String, Object>();
        String startTime = request.getParameter("startTime");//开始时间   传入的时间格式应为：yyyy-MM-dd 只到天
        String endTime = request.getParameter("endTime");//结束时间

        DateFormat df1 = new SimpleDateFormat("MM/dd/yyyy");
        Date date1 = df1.parse(startTime);
        System.out.println("date1:"+date1);
        df1 = new SimpleDateFormat("yyyy-MM-dd");
        String s1 = df1.format(date1);
        DateFormat df2 = new SimpleDateFormat("MM/dd/yyyy");
        Date date2 = df2.parse(endTime);
        df2 = new SimpleDateFormat("yyyy-MM-dd");
        String s2 = df2.format(date2);

        System.out.println(s1+"------"+s2);
        map1.put("startTime",s1);
        map1.put("endTime",s2);

        int[] title=new int[28];

        // 第一步，创建一个webbook，对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();

        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/x-download");

        String filedisplay = "标签化教评数据"+s1+"-"+s2+".xls";
//        String filedisplay = "标签化教评数据.xls";
        filedisplay = URLEncoder.encode(filedisplay, "UTF-8");
        response.addHeader("Content-Disposition", "attachment;filename="+ filedisplay);

        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("标签化教评数据");
        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow row = sheet.createRow((int) 0);
        // 第四步，创建单元格，并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

        HSSFCell cell = row.createCell((short) 0);
        cell.setCellValue("学校");
        cell=row.createCell((short) 1);
        cell.setCellValue("学院");
        cell=row.createCell((short) 2);
        cell.setCellValue("专业");
        cell=row.createCell((short) 3);
        cell.setCellValue("届数");
        cell=row.createCell((short) 4);
        cell.setCellValue("开课学院");
        cell=row.createCell((short) 5);
        cell.setCellValue("老师姓名");
        cell=row.createCell((short) 6);
        cell.setCellValue("课程名");
        cell=row.createCell((short) 7);
        cell.setCellValue("学生姓名");
        cell=row.createCell((short) 8);
        cell.setCellValue("学号");
        cell=row.createCell((short) 9);
        cell.setCellValue("评价时间");
        cell=row.createCell((short) 10);
        cell.setCellValue("周数");
        cell.setCellStyle(style);

        for (int i=1;i<title.length;i++){
            cell = row.createCell((short) 2*i+9);
            cell.setCellValue("题目"+i+"选项序号");
            cell = row.createCell((short) 2*i+10);
            cell.setCellValue("题目"+i+"选择内容");
            cell.setCellStyle(style);
        }


        // 第五步，写入实体数据 实际应用中这些数据从数据库得到，

        List<StudentEvaluate> list=evaluateStaService.selectStudentEvaluateByTimeQuantum(map1);//查出要导出的表

//        System.out.println("查到啦？？"+list.size());
        Label label=new Label();
        label.setPackagelabel("教评");
        List<Label> labelList=labelService.selectByThreeLabelName(label);
        LabelThreeIndex labelThreeIndex=new LabelThreeIndex();
        labelThreeIndex.setPackagelabel("教评");
        List<LabelThreeIndex> labelThreeIndexList=labelService.selectByTwoLabelName(labelThreeIndex);


        Map<String,Object> map2=null;
        List<Map> list1=null;

        for (int i=0;i<list.size();i++){
                //  System.out.println(list.get(i).getFilltime());

            String schoolName=list.get(i).getSchoolname();//学校
            String colleagueName=list.get(i).getColleaguename();//学院
            String studentMajor=list.get(i).getStudentmajor();//专业
            String studentGrade=list.get(i).getStudentgrade();//届数
            String courseCollege=list.get(i).getCoursecollege();//开课学院
            String teacherName=list.get(i).getTeachername();//老师姓名
            String courseName=list.get(i).getCoursename();//课程名
            String studentName=list.get(i).getStudentname();//学生姓名
            String studentId=list.get(i).getStudentid();//学号
            String evaluateTime=list.get(i).getEvaluatetime();//评价时间
            int weekNum=list.get(i).getWeeknum();//评价时间周数

            String evaluateLabel=list.get(i).getEvaluatelabel();//评价的标签

            list1=new ArrayList<Map>();

            String[] evaluateLabels=evaluateLabel.split(",");
            //System.out.println(questionOptions.length);
//            System.out.println("分割后的长度："+evaluateLabels.length);
            for (int a=0;a<evaluateLabels.length;a++){
                map2=new HashMap<String,Object>();
                String[] evaluateLabels1=evaluateLabels[a].split("!");

                String threeIndex=evaluateLabels1[1];//三级指标
                String la=evaluateLabels1[2];//标签
                int threeIndexId=0;//三级指标id  题序号
                float labelGrade=0f;//选项分数  选项序号
                for (LabelThreeIndex labelThreeIndex1:labelThreeIndexList){
                    if (threeIndex.equals(labelThreeIndex1.getThreeindexname())){
                        threeIndexId=labelThreeIndex1.getThreeindexid();
                    }
                }
                for (Label label1:labelList){
                    if (la.equals(label1.getLabelname())){
                        labelGrade=label1.getLabelgrade();
                    }
                }
//                labelThreeIndex=new LabelThreeIndex();
//                labelThreeIndex.setPackagelabel("教评");
//                labelThreeIndex.setThreeindexname(threeIndex);
//                label=new Label();
//                label.setPackagelabel("教评");
//                label.setLabelname(la);
//
//                LabelThreeIndex labelThreeIndex1=evaluateTeacherService.selectByThreeIndexName(labelThreeIndex);
//                Label label1=labelService.selectLabel(label);

                map2.put("threeIndexId",threeIndexId);//题序号
                map2.put("labelGrade",labelGrade);//选项序号
                map2.put("label",la);//选项内容

                list1.add(map2);
            }

            row = sheet.createRow((int) i + 1);
                // 第四步，创建单元格，并设置值
            row.createCell((short) 0).setCellValue(schoolName);
            row.createCell((short) 1).setCellValue(colleagueName);
            row.createCell((short) 2).setCellValue(studentMajor);
            row.createCell((short) 3).setCellValue(studentGrade);
            row.createCell((short) 4).setCellValue(courseCollege);
            row.createCell((short) 5).setCellValue(teacherName);
            row.createCell((short) 6).setCellValue(courseName);
            row.createCell((short) 7).setCellValue(studentName);
            row.createCell((short) 8).setCellValue(studentId);
            row.createCell((short) 9).setCellValue(evaluateTime);
            row.createCell((short) 10).setCellValue(weekNum);
                //  System.out.println("list1的长度："+list1.size());
//            for (int c=0;c<list1.size();c++){
//                row.createCell((short) c+1).setCellValue((Integer) list1.get(c));
//            }
//            System.out.println("list1的长度："+list1.size());
            for (Map m:list1){
                int num=(Integer)m.get("threeIndexId");

//                System.out.println(m.get("labelGrade")+"========"+m.get("label"));
                row.createCell((short) 2*num+9).setCellValue((Float)m.get("labelGrade"));
                row.createCell((short) 2*num+10).setCellValue((String)m.get("label"));
            }
        }


        // 第六步，将文件存到指定位置
        try
        {
            OutputStream out = response.getOutputStream();
            //FileOutputStream fout = new FileOutputStream("D:/SCL-90测评.xls");
            wb.write(out);
            out.close();
            map.put("code",200);
            map.put("message","导出成功");
//            map.put("data","");
        }
        catch (Exception e)
        {
            map.put("code",202);
            map.put("message","出现异常");
//            map.put("data","");
            e.printStackTrace();
        }


        return map;
    }

}
